package com.theshy.test.controller;

import com.theshy.test.dao.Student;
import com.theshy.test.dao.Teacher;
import com.theshy.test.service.TeacherService;

import java.util.Scanner;

public class TeacherController {

        TeacherService teacherService = new TeacherService();
        Scanner sc = new Scanner(System.in);
    public void start() {

        WBK:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");

            String choice = sc.next();
            switch (choice) {
                case "1":
                    AddTeacher();
                    break;
                case "2":
                    Delete();
                    break;
                case "3":
                    change();
                    break;
                case "4":
                    check();
                    break;
                case "5":
                    System.out.println("老师管理系统结束！");
                    break WBK;
                default:
                    System.out.println("输入有误！");
                    break;
            }
        }

    }

    private void check() {

       Teacher[] teachers =  teacherService.checkteacher();

       if(teachers==null){
           System.out.println("请添加老师后查看！");
           return;
       }

        for (int i = 0; i < teachers.length; i++) {
            if(teachers[i]!=null){
                System.out.println(teachers[i].getId()+"\t"+teachers[i].getName()+
                        "\t"+teachers[i].getAge()+"\t"+teachers[i].getDay());
            }
        }
    }

    private void change() {

        int id =input();

        Teacher teacher = inputTeacherinfo(id);

        teacherService.Change(teacher);

        System.out.println("修改成功！");
    }

    private void Delete() {

        int id = input();

        teacherService.Delete(id);

        System.out.println("删除成功！");
    }

    private void AddTeacher() {
        System.out.println("请输入ID:");
        int id;
        while (true) {
            id = sc.nextInt();
            boolean bo = teacherService.isExist(id);
            if (bo) {
                System.out.println("id存在请重新输入");
            } else {
                break;
            }
        }

        Teacher t = inputTeacherinfo(id);

       boolean b =teacherService.AddTeacher(t);
       
       if(b){
           System.out.println("添加成功！");
       }else{
           System.out.println("添加失败！老师已满！");
       }

    }
    public int input() {


        System.out.println("请输入ID:");
        int id;
        while (true) {

            id = sc.nextInt();
            boolean bo = teacherService.isExist(id);
            if (bo) {
                break;
            } else {
                System.out.println("id不存在请重新输入");
            }
        }

        return id;
    }

    public Teacher inputTeacherinfo(int id) {


        System.out.println("请输入姓名:");

        String name = sc.next();

        System.out.println("请输入年龄:");
        int age = sc.nextInt();

        System.out.println("请输入生日:");
        String day = sc.next();

        Teacher t = new Teacher();
        t.setId(id);
        t.setAge(age);
        t.setName(name);
        t.setDay(day);

        return t;
    }
}
